package source

import (
	"encoding/json"
	"fmt"
	"net/url"
	"time"
)

const (
	TencentAiBaseUrl = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttrans"
)

type TencentAiData struct {
	Org_text   string
	Trans_text string
}

type TencentAiResp struct {
	Ret  int
	Msg  string
	Data TencentAiData
}

// 腾讯AI源翻译
func TranslateTencentAi(q, fromType string) []string {
	var wordMeans []string

	timestamp := fmt.Sprintf("%d", time.Now().Unix())
	randStr := GetRandomSalt()

	params := map[string]string{
		"app_id":     TencentAppid,
		"type":       "0",
		"text":       q,
		"time_stamp": timestamp,
		"nonce_str":  randStr,
	}
	params["sign"] = getReqSign(params)
	var query string
	for k, v := range params {
		value := url.QueryEscape(v)
		query += fmt.Sprintf("%s=%s&", k, value)
	}

	url := TencentAiBaseUrl + "?" + query
	body, err := GetJson(url)
	if err != nil {
		fmt.Println("返回结果出错", err)
	}

	var r TencentAiResp
	if err2 := json.Unmarshal(body, &r); err2 != nil {
		fmt.Println("返回结果出错", err2)
	}

	results := r.Data //获取翻译结果
	wordMeans = append(wordMeans, results.Trans_text)

	return wordMeans
}
